package com.sky.service.impl;

import com.sky.context.UserHolder;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartSerivce;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartSerivceImpl implements ShoppingCartSerivce {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    //添加到购物车
    @Override
    public void save(ShoppingCart shoppingCart) {
        //1. 根据当前用户查询是否点过当前菜品或者是套餐
        shoppingCart.setUserId(UserHolder.get());//设置用户id
        ShoppingCart shoppingCartFromDB = shoppingCartMapper.findOne(shoppingCart);

        //2. 判断
        if (shoppingCartFromDB != null) {
            //3-1. 点过,数量+1
            shoppingCartFromDB.setNumber(shoppingCartFromDB.getNumber() + 1);//数量+1
            //3-2. 更新
            shoppingCartMapper.update(shoppingCartFromDB);
        } else {
            //4 没点过,先判断,添加到底是菜品还是套餐
            if (shoppingCart.getSetmealId() != null) {
                //套餐
                //4-1 先根据套餐id,查询套餐基本信息
                SetmealVO setmealVO = setmealMapper.findById(shoppingCart.getSetmealId());
                //4-2 补齐对象参数
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                //4-3 保存
                shoppingCartMapper.insert(shoppingCart);
            } else {
                //5菜品
                //5-1 先根据菜品id,查询菜品基本信息
                DishVO dishVO = dishMapper.findById(shoppingCart.getDishId());
                //5-2 补齐对象参数
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dishVO.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                //5-3 保存
                shoppingCartMapper.insert(shoppingCart);
            }
        }
    }

    // 查询购物车
    @Override
    public List<ShoppingCart> findList() {

        // 获取登录人 id
        Long userId = UserHolder.get();

        // 根据用户id查询购物车
        return shoppingCartMapper.findList(userId);

    }

    // 清空购物车
    @Override
    public void clean() {
        // 1 获取登录人id
        Long userId = UserHolder.get();

        //2 根据用户id删除购物车数据
        shoppingCartMapper.delete(userId);
    }

    // 删减少购物车
    @Override
    public void sub(ShoppingCart shoppingCart) {
        //1.根据用户id,菜品id 或 套餐id查询购物车数据
        shoppingCart.setUserId(UserHolder.get());
        ShoppingCart shoppingCartFromDB = shoppingCartMapper.findOne(shoppingCart);
        //2.判断
        if (shoppingCartFromDB.getNumber() == 1) {
            //3-1.如果数量为1,直接删除
            shoppingCartMapper.delete(shoppingCartFromDB.getId());
        } else {
            //3-2.数量不为1,数量-1
            shoppingCartFromDB.setNumber(shoppingCartFromDB.getNumber() - 1);
            //3-3.更新
            shoppingCartMapper.update(shoppingCartFromDB);
        }
    }

}
